package com.roc.base.mall.configuration.db;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = SubDataSourceConfigurer.PACKAGE, sqlSessionFactoryRef = "subSqlSessionFactory", sqlSessionTemplateRef = "subSqlSessionTemplate")
public class SubDataSourceConfigurer {

    static final String PACKAGE = "com.roc.base.mall.dao.sub";
    /**
     * SqlSessionFactory
     *
     * @return
     * @throws Exception
     */

    @Bean(name = "subSqlSessionFactory")
    public SqlSessionFactory NewErpSqlSessionFactory(@Qualifier("subDataSource") DataSource subDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(subDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/sub/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean(name = "subSqlSessionTemplate")
    public SqlSessionTemplate newSqlSessionTemplate(
            @Qualifier("subSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


}
